<template>
  <!-- 从业人员资质 -->
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="auto" size="small">
      <el-form-item label="更新时间" prop="releaseTime">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="-"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="从业证件类型" prop="practiceCertType">
        <el-select v-model="queryParams.practiceCertType" clearable placeholder="请选择从业证件类型">
          <el-option
            label="全部"
            :value="null"
          />
          <el-option
            v-for="dict in dict.type.bhxq_employee_practice_cert_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="证件状态" prop="postStatus">
        <el-select v-model="queryParams.postStatus" clearable placeholder="请选择证件状态">
          <el-option
            v-for="dict in dict.type.bhxq_employee_post_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="employeeId">
        <el-select v-model="queryParams.employeeId" clearable filterable placeholder="请搜索选择从业人员" style="width: 217px">
          <el-option
            v-for="dict in personnel"
            :key="dict.employeeId"
            :label="dict.name"
            :value="dict.employeeId"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:aptitude:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-upload
            class="upload-demo"
            style="float: right"
            :show-file-list="false"
        >
          <el-button size="mini" icon="el-icon-download" plain type="warning">导入</el-button>
        </el-upload>
      </el-col>-->
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="aptitudeList" @selection-change="handleSelectionChange">
      <el-table-column align="center" label="姓名" prop="name" />
      <el-table-column align="center" label="从业证件类型" prop="practiceCertType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bhxq_employee_practice_cert_type" :value="scope.row.practiceCertType" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="证件状态" prop="postStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bhxq_employee_post_status" :value="scope.row.postStatus" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新时间" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="吊销原因" align="center" prop="revokeReason" />-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleexamine(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改人员资质对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="80%">
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="姓名" prop="employeeId">
              <el-select
                v-model="form.employeeId"
                :disabled="disabledon"
                filterable
                placeholder="请搜索选择姓名"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in personnel"
                  :key="dict.employeeId"
                  :label="dict.name"
                  :value="dict.employeeId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="从业证件类型" prop="practiceCertType">
              <el-select
                v-model="form.practiceCertType"
                :disabled="disabledon"
                placeholder="请选择从业证件类型"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.bhxq_employee_practice_cert_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="从业资格证编号" prop="practiceCertNum">
              <el-input
                v-model="form.practiceCertNum"
                :disabled="disabledon"
                placeholder="请输入从业资格证编号"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="从业证件初次发证日期" prop="firstDate">
              <el-date-picker
                v-model="form.firstDate"
                :disabled="disabledon"
                clearable
                placeholder="请选择从业证件初次发证日期"
                type="date"
                value-format="yyyy-MM-dd"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="从业证件本次发证日期" prop="currentsDate">
              <el-date-picker
                v-model="form.currentsDate"
                :disabled="disabledon"
                clearable
                placeholder="请选择从业证件本次发证日期"
                type="date"
                value-format="yyyy-MM-dd"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="从业证件有效截至日期" prop="deadlineDate">
              <el-date-picker
                v-model="form.deadlineDate"
                :disabled="disabledon"
                clearable
                placeholder="请选择从业证件有效截至日期"
                type="date"
                value-format="yyyy-MM-dd"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="从业证发证机构" prop="practiceCertOrg">
              <el-input
                v-model="form.practiceCertOrg"
                :disabled="disabledon"
                placeholder="请输入从业证发证机构"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="证件当前状态" prop="postStatus">
              <el-select
                v-model="form.postStatus"
                :disabled="disabledon"
                placeholder="请选择证件当前状态"
                style="width: 100%"
                @change="statusChange"
              >
                <el-option
                  v-for="dict in dict.type.bhxq_employee_post_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="证件吊销原因" prop="revokeReason">
              <el-input
                v-model="form.revokeReason"
                :disabled="form.postStatus == undefined || form.postStatus == '1' || title === '查看从业人员资质'"
                placeholder="请输入证件吊销原因"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!disabledon" v-no-more-click type="primary" @click="submitForm">确 定</el-button>
        <el-button v-if="!disabledon && title !== '修改从业人员资质'" type="primary" @click="submitForm('ContinueFill')">确定并继续填写</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { listAptitude, getAptitude, delAptitude, addAptitude, updateAptitude, getpersonnel } from '@/api/bhxq/aptitude'

import {
  addAptitude,
  delAptitude,
  getAptitude,
  getpersonnel,
  listAptitude,
  updateAptitude
} from '@/api/informationReport'

export default {
  name: 'Index',
  dicts: ['bhxq_employee_practice_cert_type', 'bhxq_employee_post_status'],
  data() {
    return {
      pracTypeList: [],
      personnel: [],
      disabledon: true,
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员资质表格数据
      aptitudeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeId: null,
        enterpriseId: null,
        businessType: null,
        practiceCertType: null,
        practiceCertNum: null,
        firstDate: null,
        currentsDate: null,
        deadlineDate: null,
        practiceCertOrg: null,
        postStatus: null,
        revokeReason: null
      },
      queryParamslist: {
        employeeId: null,
        businessType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        employeeId: [
          { required: true, message: '姓名不能为空', trigger: 'change' }
        ],
        enterpriseId: [
          { required: true, message: '企业ID不能为空', trigger: 'blur' }
        ],
        businessType: [
          { required: true, message: '类型不能为空', trigger: 'change' }
        ],
        practiceCertType: [
          { required: true, message: '从业证件类型不能为空', trigger: 'change' }
        ],
        practiceCertNum: [
          { required: true, message: '从业资格证编号不能为空', trigger: 'blur' }
        ],
        firstDate: [
          { required: true, message: '初次发证日期不能为空', trigger: 'blur' }
        ],
        currentsDate: [
          { required: true, message: '本次发证日期不能为空', trigger: 'blur' }
        ],
        deadlineDate: [
          { required: true, message: '有效截至日期不能为空', trigger: 'blur' }
        ],
        practiceCertOrg: [
          { required: true, message: '从业证发证机构不能为空', trigger: 'blur' }
        ],
        postStatus: [
          { required: true, message: '证件当前状态不能为空', trigger: 'change' }
        ]

      }
    }
  },
  created() {
    this.getList()
    this.getpersonnellist()
  },
  methods: {
    statusChange() {
      this.form.revokeReason = ''
    },
    // 查询人员列表
    getpersonnellist() {
      this.queryParamslist.businessType = 1
      getpersonnel(this.queryParamslist).then(response => {
        this.personnel = response.data
      })
    },
    /** 查询人员资质列表 */
    getList() {
      this.loading = true
      // this.queryParams.businessType = 1
      if (this.dateRange == null) {
        this.dateRange = []
      }
      const data = {
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
        employeeId: this.queryParams.employeeId,
        practiceCertType: this.queryParams.practiceCertType,
        postStatus: this.queryParams.postStatus,
        updateTimeStart: this.dateRange.length === 0 ? undefined : this.dateRange[0],
        updateTimeEnd: this.dateRange.length === 0 ? undefined : this.dateRange[1]
      }
      listAptitude(data).then(response => {
        this.aptitudeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        employeeId: null,
        enterpriseId: null,
        businessType: null,
        practiceCertType: null,
        practiceCertNum: null,
        firstDate: null,
        currentsDate: null,
        deadlineDate: null,
        practiceCertOrg: null,
        postStatus: null,
        revokeReason: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      }

      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = [],
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.disabledon = false
      this.getpersonnellist()
      this.open = true
      this.title = '新增从业人员资质'
    },

    /** 查看按钮操作 */
    handleexamine(row) {
      this.reset()
      const id = row.id || this.ids
      getAptitude(id).then(response => {
        this.form = response.data
        this.disabledon = true
        this.open = true
        this.title = '查看从业人员资质'
      })
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAptitude(id).then(response => {
        this.form = response.data
        this.disabledon = false
        this.open = true
        this.title = '修改从业人员资质'
      })
    },
    /** 提交按钮 */
    submitForm(type) {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.businessType = 1
            updateAptitude(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            this.form.businessType = 1
            addAptitude(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              // 当点击确认确定并继续填写时 模态框不关闭
              if (type === 'ContinueFill') {
                this.reset()
                this.open = true
              } else {
                this.open = false
              }
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除人员资质编号为"' + row.practiceCertNum + '"的数据项？').then(function() {
        return delAptitude(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/background/bhxqEmployeeAptitude/export', {
        ...this.queryParams
      }, `aptitude_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
